package com.guoyh.bilbase.ext

import android.view.View
import androidx.annotation.DrawableRes
import com.guoyh.bilbase.R
import com.guoyh.baseutils.ext.getDrawablem
import com.guoyh.bilbase.widget.state.MStateCommon
import com.guoyh.bilbase.widget.state.MStateError
import com.guoyh.bilbase.widget.state.MStateLoading
import com.guoyh.baseutils.utils.NetWorkUtil
import com.zy.multistatepage.MultiStateContainer
import com.zy.multistatepage.MultiStatePage
import com.zy.multistatepage.OnRetryEventListener
import com.zy.multistatepage.state.SuccessState

/**
 * @CreateDate: 7/14/21 11:54 AM
 * @Author: guoyh
 * @Description: 状态布局
 */

///状态绑定
fun View.initStatPage(toRtrey: (MultiStateContainer) -> Unit) =
    MultiStatePage.bindMultiState(this, object : OnRetryEventListener {
        override fun onRetryEvent(multiStateContainer: MultiStateContainer) {
            toRtrey.invoke(multiStateContainer)
        }
    })


///转态切换----空布局
fun MultiStateContainer.showEmpty(
    msg: String = "暂无相关数据！",
    @DrawableRes drawableRes: Int = R.drawable.state_empty
) {
    show<MStateCommon>() {
        it.setImg(getDrawablem(drawableRes))
        it.setTv(msg)
    }
}

///转态切换----空布局
fun MultiStateContainer.showEmptyAnBtn(
    msg: String,
    @DrawableRes drawableRes: Int = R.drawable.state_empty,
    btns: String,
    btnClick: () -> Unit
) {
    show<MStateCommon>() {
        it.setImg(getDrawablem(drawableRes))
        it.setTv(msg)
        it.setBtn(btns, btnClick)
    }
}

///转态切换----Loading布局
fun MultiStateContainer.showLoading(msg: String = "请稍后...") {
    show<MStateLoading>(enableAnimator = false) {
    }
}

///转态切换----Error布局
fun MultiStateContainer.showError(
    msg: String = "数据库遇袭，请稍后再试！",
    @DrawableRes drawableRes: Int = R.drawable.state_error
) {
    if (NetWorkUtil.isNetworkAvailable) {
        show<MStateError>() {
            it.setImg(getDrawablem(drawableRes))
            it.setTv(msg)
        }
    } else {
        showNoNet()
    }

}

///转态切换----无网 布局
fun MultiStateContainer.showNoNet(
    msg: String = "网络走丢了，点击刷新一下",
    @DrawableRes drawableRes: Int = R.drawable.state_nonetwork
) {
    show<MStateError>() {
        it.setImg(getDrawablem(drawableRes))
        it.setTv(msg)
    }
}

///状态切换----成功
fun MultiStateContainer.showSuccess() {
    show<SuccessState>()
}



